var htmlObj = {
    activity: 'activity.html',
    merchant: 'merchant.html',
    goods: 'goodsInfo.html',
    allGoods: 'merchantGoods.html',
    category: 'category.html'
};
var imgObj = {
    goods: 'img/goods.jpg',
    activity: 'img/activity_pic.jpg',
    goodsInfo: 'img/goods_info1.jpg',
    merchant: 'img/activity_pic.jpg',
    merchantPics: 'img/merchant_pic.jpg'
};
var _hmt = _hmt || [];
(function() {
    var hm = document.createElement("script");
    hm.src = "http://hm.baidu.com/hm.js?6bca56789989d0403db1f698d57bbce2";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
})();
(function (doc, win) {
    var docEl = doc.documentElement,
        resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
        recalc = function () {
            var clientWidth = docEl.clientWidth;
            if (!clientWidth) return;
            docEl.style.fontSize = 8.55 * (clientWidth / 320) + 'px';
        };

    if (!doc.addEventListener) return;
    win.addEventListener(resizeEvt, recalc, false);
    doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);
var baseUrl = "http://123.56.90.38:8080/FPHome/";
var scriptFlag = true;
var pn = 0;
var rn = 1;
var currentType = "";
var urlObj = {
    category: function(){
        return "business/list?callback=showMerchantList&catId="+getLocationKey("id",1)+"&pn="+pn+"&rn="+rn;
    },
    merchantGoods: function(){
        return "product/list?callback=showGoodsList&busiId="+getLocationKey("id",1)+"&pn="+pn+"&rn="+rn;
    },
    merchantPics: function(){
        return "business/pics?callback=showPics&busiId="+getLocationKey("id",1)+"&pn="+pn+"&rn="+rn;
    }
};
function loadData(type){
    if(type){
        currentType = type;
    }
    if($(".loading").size()>0){
        return;
    }
    pn++;
    loadingStart();
    getScriptData(urlObj[currentType]());
}
function getScriptData(callbackUrl){
    var url;
    var callbackName = getValue(callbackUrl,'callback');
    if(callbackUrl.indexOf("?callback=")>-1){
        url = callbackUrl.replace("?callback="+callbackName,"?random="+Math.random());
    }else if(callbackUrl.indexOf("&callback=")>-1){
        url = callbackUrl.replace("&callback="+callbackName,"&random="+Math.random());
    }
    url = baseUrl + url;
    if(scriptFlag){
        $.getScript(baseUrl + callbackUrl);
    }else{
        $.ajax({
            url: url,
            type: 'get',
            dataType: 'json',
            success: function(json){
                if(json&&json.desc==="success"){
                    loadingEnd();
                    eval(callbackName).call(null,json.data);
                }
            }
        });
    }
}
function getLocationKey(key,defaultValue){
    var val = getValue(window.location.href,key);
    if(!val&&defaultValue){
        val = defaultValue;
    }
    return val;
}
function getValue(url,key){
    url = url.toString();
    if(url.indexOf('#')>=0){
        url = url.substring(0,url.length-1);
    }
    var value='';
    var begin = url.indexOf(key + '=');
    if(begin>=0){
        var tmp = url.substring(begin + key.length + 1);
        var eqIdx = tmp.indexOf('=');
        var end = 0;
        if(eqIdx>=0){
            tmp = tmp.substring(0,eqIdx);
            end = tmp.lastIndexOf('&');
        }else{
            end = tmp.length;
        }
        if(end>=0){
            try{
                value = decodeURIComponent(tmp.substring(0,end));
            }catch(e){
                value = tmp.substring(0,end);
            }
        }else{
            try{
                value = decodeURIComponent(tmp);
            }catch(e){
                value = tmp;
            }
        }
    }
    return value;
}
function showSlide(){
    var swiper = $('.swiper-container');
    if(swiper.size()==0){
        return;
    }
    var swImg = swiper.find('img');
    var win = $(window).width();
    var imgHeight = win/1.12;
    swImg.height(imgHeight);
    swiper.height(imgHeight);
    var newSwiper = new Swiper('.swiper-container', {
        pagination: '.goods-slider',
        loop: true,
        grabCursor: true,
        autoplay: 5000,
        paginationClickable: true
    });
    swiper.find('.swiper-wrapper').show();
}
function loadingStart(){
    var html = '';
    html += '<div class="loading">';
    html += '<div class="loading-bg"></div>';
    html += '<div class="spinner">';
    html += '<div class="spinner-container container1">';
    html += '<div class="circle1"></div>';
    html += '<div class="circle2"></div>';
    html += '<div class="circle3"></div>';
    html += '<div class="circle4"></div>';
    html += '</div>';
    html += '<div class="spinner-container container2">';
    html += '<div class="circle1"></div>';
    html += '<div class="circle2"></div>';
    html += '<div class="circle3"></div>';
    html += '<div class="circle4"></div>';
    html += '</div>';
    html += '<div class="spinner-container container3">';
    html += '<div class="circle1"></div>';
    html += '<div class="circle2"></div>';
    html += '<div class="circle3"></div>';
    html += '<div class="circle4"></div>';
    html += '</div>';
    html += '</div>';
    html += '</div>';
    $(".loading").remove();
    $("body").append(html);
}
function loadingEnd(){
    $(".loading").remove();
    $("body").css("visibility","visible");
}
function showMore(json){
    try{
        if(json.data.totalPage > pn){
            $(".more").css("visibility","visible");
        }else{
            $(".more").css("visibility","hidden");
        }
    }catch(e){}
}
$(function(){
    $("body").on("click","[data-go]",function(){
        var dataStr = $(this).attr("data-go");
        var htmlName = dataStr.split("-")[0];
        var id;
        try{
            id = dataStr.split("-")[1];
        }catch(e){}
        var href;
        if(id){
            href = htmlName+"?id="+id;
        }else{
            href = htmlName;
        }
        window.location.href = href;
    });
    $(".more").click(function(){
        try{
            loadData();
        }catch(e){}
    });
});